/*
 * @Description:
 * @Autor: dongjunhua
 * @Date: 2021-02-01 10:38:02
 * @LastEditors: dongjunhua
 * @LastEditTime: 2021-09-04 11:34:07
 */
import Storage from '@/utils/Storage';

/**
 * @class App
 */
class App {
  constructor(
    recognizeId,
    applicationName,
    releaseCycle,
    version,
    coreVersion,
    device,
    layout,
    sidebar = {
      enabled: true,
      showLogo: false,
      opened: true,
      withoutAnimation: true
    },
    navbar = {
      enabled: true
    },
    navigation = {
      enabled: false
    },
    tagsView = {
      enabled: true,
      type: 'default',
      cachedViews: []
    },
    settings = {
      enabled: true
    },
    themes = [],
    enableDynamicBg
  ) {
    this.recognizeId = recognizeId; //应用识别id
    this.applicationName = applicationName; //应用名称
    this.releaseCycle = releaseCycle; //版本
    this.version = version; //版本号
    this.coreVersion = coreVersion; //Vue版本号
    this.device = device; //环境标识
    this._layout = layout; //布局
    this.sidebar = sidebar; //侧边栏
    this.navbar = navbar; //导航栏
    this.navigation = navigation; //导航信息栏
    this.tagsView = tagsView; //标签页
    this.settings = settings; //设置栏
    this.themes = themes; //主题列表
    this.enableDynamicBg = enableDynamicBg; //是否开启登录动态背景
    this.loading = false; //全局加载
    this.destoryRoute = null; //需要取消缓存的路由
    this._setLayout();
  }

  get layout() {
    return this._layout;
  }
  set layout(value) {
    this._layout = value;
    this._setLayout();
  }

  /**
   * @method loadConfig
   * @for App
   * @param {object} config
   * @param {Array} themeConfigs
   */
  loadConfig(config, themeConfigs = []) {
    // 加载基础配置
    config.recognizeId && (this.recognizeId = config.recognizeId);
    config.applicationName && (this.applicationName = config.applicationName);
    config.releaseCycle && (this.releaseCycle = config.releaseCycle);
    config.version && (this.version = config.version);
    config.coreVersion && (this.coreVersion = config.coreVersion);
    config.device && (this.device = config.device);
    if (config.sidebar) {
      this.sidebar.enabled = !!config.sidebar.enabled;
      this.sidebar.showLogo = !!config.sidebar.showLogo;
    }
    config.navbar && (this.navbar.enabled = !!config.navbar.enabled);
    config.navigation &&
      (this.navigation.enabled = !!config.navigation.enabled);
    if (config.tagsView) {
      this.tagsView.enabled = !!config.tagsView.enabled;
      this.tagsView.type = config.tagsView.type;
    }
    config.settings && (this.settings.enabled = !!config.settings.enabled);
    this.enableDynamicBg = !!config.enableDynamicBg;
    // 加载主题配置
    let activeIndex = themeConfigs.findIndex(t => t.default);
    activeIndex < 0 && (activeIndex = 0);
    this.themes = themeConfigs.map((t, i) => {
      return {
        name: t.name,
        titleName: t.titleName,
        styleColor: t.styleColor,
        active: i === activeIndex
      };
    });
  }

  /**
   * @method saveStorage
   * @for App
   */
  saveStorage() {
    let sidebar = {
      enabled: this.sidebar.enabled,
      showLogo: this.sidebar.showLogo
    };
    let tagsView = {
      enabled: this.tagsView.enabled,
      type: this.tagsView.type
    };
    Storage.setLocal('app', {
      layout: this.layout,
      sidebar,
      navbar: this.navbar,
      navigation: this.navigation,
      tagsView,
      settings: this.settings,
      themes: this.themes,
      enableDynamicBg: this.enableDynamicBg
    });
    sidebar = {
      opened: this.sidebar.opened
    };
    tagsView = {
      cachedViews: this.tagsView.cachedViews
    };
    Storage.setSession('app', { sidebar, tagsView });
  }

  /**
   * @method loadStorage
   * @for App
   */
  loadStorage() {
    const localStorage = Storage.getLocal('app');
    if (localStorage) {
      this.layout = localStorage.layout;
      this.sidebar.enabled = localStorage.sidebar.enabled;
      this.sidebar.showLogo = localStorage.sidebar.showLogo;
      this.navbar = localStorage.navbar;
      this.navigation = localStorage.navigation;
      this.tagsView.enabled = localStorage.tagsView.enabled;
      this.tagsView.type = localStorage.tagsView.type;
      this.settings = localStorage.settings;
      this.themes = localStorage.themes;
      this.enableDynamicBg = localStorage.enableDynamicBg;
    }
    const sessionStorage = Storage.getSession('app');
    if (sessionStorage) {
      this.sidebar.opened = sessionStorage.sidebar.opened;
      this.tagsView.cachedViews = sessionStorage.tagsView.cachedViews;
    }
  }

  /**
   * @method applyTheme
   * @for App
   */
  applyTheme() {
    const activeTheme = this.themes.find(t => t.active);
    if (activeTheme) {
      const styleColor = activeTheme.styleColor;
      let varCss = '';
      for (let key of Object.keys(styleColor)) {
        varCss += `--color-${key}:${styleColor[key]};`;
      }
      varCss += `--menu-border-width:${
        activeTheme.name === 'light' ? '1' : '0'
      }px;`;
      document.documentElement.style.cssText = varCss;
    }
  }

  /**
   * @internal
   * @method _setLayout
   * @for App
   */
  _setLayout() {
    if (!this._layout) {
      return;
    }
    switch (this._layout) {
      case 'top':
        this.sidebar.enabled = false;
        this.navbar.enabled = true;
        break;
      case 'hidden':
        this.sidebar.enabled = false;
        this.navbar.enabled = false;
        break;
      default:
        this.sidebar.enabled = true;
        this.navbar.enabled = false;
        break;
    }
  }
}

export default App;
